$(function(){
	pc();
	findNews(1);
})
//查总页数
function pc(){
	$.ajax({
		"url":"news",
		"type":"post",
		"data":"opr=findNews&pc=pc",
		"dataType":"text",
		"success":function(data){
			pageCount = Number(data);
			paging();
		}
	})
}
var pageIndex = 1;
var pageCount = 0;
function findNews(pi){
	pageIndex = pi;
	$("#news").empty();
	$("#news").append("<tr><th>标题</th><th>内容</th><th>录入日期</th></tr>");
	$.ajax({
		"url":"news",
		"type":"post",
		"data":"opr=findNews&pageIndex="+pi,
		"dataType":"json",
		"success":function(data){
			for(i=0;i<data.length;i++){
				var str = "<tr><td>"+data[i].title+"</td><td>"+data[i].content+"</td><td>"+data[i].createTime+"</td></tr>";
				$("#news").append(str);
			}
		}
	})
	paging();
}
function paging(){
	$("#paging").empty();
	$("#paging").append("<a href='javascript:findNews(1)' class='p_pre'>首页</a>");
	if(pageIndex == 1 && pageCount == 1){
		$("#paging").append(pageNum());
	} else if (pageIndex == 1){
		$("#paging").append(pageNum());
		$("#paging").append("<a href='javascript:findNews(pageIndex+1)' class='p_pre'>下一页</a>");
	} else if (pageIndex == pageCount){
		$("#paging").append("<a href='javascript:findNews(pageIndex-1)' class='p_pre'>上一页</a>");
		$("#paging").append(pageNum());
	} else {
		$("#paging").append("<a href='javascript:findNews(pageIndex-1)' class='p_pre'>上一页</a>");
		$("#paging").append(pageNum());
		$("#paging").append("<a href='javascript:findNews(pageIndex+1)' class='p_pre'>下一页</a>");
	}
	$("#paging").append("<a href='javascript:findNews(pageCount)' class='p_pre'>尾页</a>");
	bs();
}
function pageNum(){
	var str = "";
	if(pageCount < 6){
		for(i=1;i<=pageCount;i++){
			str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
		}
	} else {
		for(i=1;i<=pageCount;i++){
			if(pageIndex == 1){
				if(i>3 && i< pageCount-1){
					continue;
				}
				if(i == pageCount-1){
					str += "...";
				}
				str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
			} else if (pageIndex <= pageCount - 3) {
				if(pageIndex >= (i-1) && pageIndex<=(i+1)){
					str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
				}
				if(i == pageCount -1){
					str += "...";
				}
				if(i >= pageCount-1){
					str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
				}
			} else if (pageIndex == pageCount - 2){
				if(i > pageIndex-3 && i <= pageIndex){
					str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
				}
				if(i == pageIndex){
					str += "...";
				}
				if(i >pageCount -2){
					str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
				}
			} else if (pageIndex >= pageCount - 3){
				if(i<=3){
					str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
				}
				if(i==3){
					str += "...";
				}
				if(i > pageCount - 2){
					str += "<a href='javascript:findNews("+i+")'>"+i+"</a>";
				}
			}
		}
	}
	return str;
}
function bs(){
	$("#paging a").each(function(){
		if(this.text==pageIndex){
			$(this).css({"background-color":"#FF4E00","color":"#fff"});
		}
	})
}